Skip to content

Qualcomm AI Engine Direct - Refactor llama runner #10578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025

Conversation

shewu-quic
Copy link
Collaborator

@shewu-quic shewu-quic commented Apr 30, 2025

Summary:

  • Refactored io_manager into five distinct components:
    • DecoderRunner: Module wrapper class.
    • PromptProcessor: Handles prompt processing using the decoder and key-value manager.
    • TokenGenerator: Generates tokens using the decoder and key-value manager.
    • KVManager: Manages key-value cache with kv_updater, including data buffer allocation, cache updates, and buffer updates in TensorImpl.
    • IBufferAlloc: Allocates data buffers from RPC memory or client buffer.
    • Validated story llama with CL=128, prefill_ar_len=16, QNN SDK: 2.32
    • Original :
CL prefill_ar_len eval_mode kv_updater Model Load Time (seconds) Prompt evaluation (seconds) Generated token rate (tokens/seconds) Time to first generated token (seconds)
128 16 KV shift_pointer 0.3082 0.0105 237.5553131 0.0152
128 16 KV smart_mask 0.2691 0.0501 258.9103433 0.0544
128 16 hybrid shift_pointer 0.3408 0.008 232.1754892 0.008
128 16 hybrid smart_mask 0.3175 0.0447 237.7134587 0.0447
  • Refactor:
CL prefill_ar_len eval_mode kv_updater Model Load Time (seconds) Prompt evaluation (seconds) Generated token rate (tokens/seconds) Time to first generated token (seconds)
128 16 KV shift_pointer 0.2808 0.0124 234.835 0.0124
128 16 KV smart_mask 0.238 0.027 251.004016 0.027
128 16 hybrid shift_pointer 0.3305 0.0082 229.1122162 0.0082
128 16 hybrid smart_mask 0.258 0.013 239.463602 0.013
  • Support multi-turn use case.
    • Validated on story llama. To simulate the scenario, I forced decode mode to generate 5 tokens each time. Tokens with random length are inserted after one round of prefill->decode finished.
    • Reproduce command: (Note that some whitespaces are missing due to decoding. But token is actually the same as golden.)
    python examples/qualcomm/oss_scripts/llama/llama.py -b build-android --checkpoint stories110M.pt --params params.json --tokenizer_model tokenizer.model --prompt "Once" "a little girl named Lily." "toys and her favorite toy was a big, red ball." "s mom asked her to help her with the laundry." "and she put all the clothes in the washing machine." --temperature 0 --tokenizer_bin tokenizer.bin --llama_model stories110m --model_mode hybrid --ptq 16a4w -m SM8650 -H ${HOST} -s ${DEVICE}-a ${ARTIFACTS}--max_seq_len 128 --prefill_ar_len 16
    Result:
    Once upon a time, there wasa little girl named Lily. She loved to play with hertoys and her favorite toy was a big, red ball. One day, Lily's mom asked her to help her with the laundry. Lily was happy to helpand she put all the clothes in the washing machine.
    After the clothes were
    
    • Need to apply the below patch to forced decode mode to generate 5 tokens each time.
diff --git a/examples/qualcomm/oss_scripts/llama/runner/token_generator.cpp b/examples/qualcomm/oss_scripts/llama/runner/token_generator.cpp
index 8a81b598d..a8ec53cdb 100644
--- a/examples/qualcomm/oss_scripts/llama/runner/token_generator.cpp
+++ b/examples/qualcomm/oss_scripts/llama/runner/token_generator.cpp
@@ -170,7 +170,10 @@ Result<int64_t> TokenGenerator::generate(
       "Failed to set output tensor for module %s",
       forward_name_.c_str());
   // Generate our tokens
-  while (pos < seq_len - 1) {
+  // force decode to generate 5 runs at most
+  int64_t max_pos = std::min(pos + 5, (int64_t)seq_len - 1);
+//   while (pos < seq_len - 1) {
+  while (pos < max_pos) {
     // Fill in the token and position data
     prepare_io(cur_token, pos);
     // Only update data pointer of the cache to the tensor for SHIFT_POINTER

@shewu-quic shewu-quic requested a review from cccclai as a code owner April 30, 2025 06:41
Copy link

pytorch-bot bot commented Apr 30, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/10578

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit a876626 with merge base c5dd476 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 30, 2025
@shewu-quic
Copy link
Collaborator Author

shewu-quic commented Apr 30, 2025

Hi @cccclai,

This PR is to refactor llama runner which is an infra change for adopting lookahead decoding and to support multi-turn conversation.
I have tested accuracy and performance on story llama 110M.
Could you please help take a look?

Thanks

Summary:
- Refactored io_manager into five distinct components:
  - DecoderRunner: Module wrapper class.
  - PromptProcessor: Handles prompt processing using the decoder and key-value manager.
  - TokenGenerator: Generates tokens using the decoder and key-value manager.
  - KVManager: Manages key-value cache with kv_updater, including data buffer allocation, cache updates, and buffer updates in TensorImpl.
  - IBufferAlloc: Allocates data buffers from RPC memory or client buffer.
- Support multi-turn use case. Validate on story llama
  - To simulate the scenario, I forced decode mode to generate 5 tokens each time. Tokens with random length are inserted after one round of prefill->decode finished.
@shewu-quic shewu-quic force-pushed the dev1/hutton/refactor_llama_runner branch from 4472c56 to a876626 Compare April 30, 2025 06:58
@cccclai
Copy link
Contributor

cccclai commented May 28, 2025

Hey sorry for being late on this PR, can you help rebasing?

@facebook-github-bot
Copy link
Contributor

@cccclai has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@cccclai cccclai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@cccclai cccclai added the release notes: qualcomm Changes to the Qualcomm backend delegate label May 28, 2025
@cccclai cccclai merged commit d8ac866 into pytorch:main May 28, 2025
92 of 93 checks passed
@cccclai
Copy link
Contributor

cccclai commented May 28, 2025

If I apply

./qnn_llama3_2_runner --model_path hybrid_stories_qnn.pte    --tokenizer_path tokenizer.bin  --eval_mode 1 --kv_updater "ShiftPointer" --output_path output.txt --num_iters 1 --seq_len 512 --prompt "Once" "a little girl named Lily." "toys and her favorite toy was a big, red ball." "s mom asked her to help her with the laundry." "and she put all the clothes in the washing machine." --temperature 0 

I got the results as "Once upon a time, there was", is it expected?

cccclai added a commit to cccclai/executorch-1 that referenced this pull request May 29, 2025
Summary: Forward fix for pytorch#10578

Reviewed By: kimishpatel

Differential Revision: D75536694
@shewu-quic
Copy link
Collaborator Author

If I apply

./qnn_llama3_2_runner --model_path hybrid_stories_qnn.pte    --tokenizer_path tokenizer.bin  --eval_mode 1 --kv_updater "ShiftPointer" --output_path output.txt --num_iters 1 --seq_len 512 --prompt "Once" "a little girl named Lily." "toys and her favorite toy was a big, red ball." "s mom asked her to help her with the laundry." "and she put all the clothes in the washing machine." --temperature 0 

I got the results as "Once upon a time, there was", is it expected?

To run multiple prompts with ./qnn_llama3_2_runner, you should use the --prompt flag for each prompt. For example:

./qnn_llama3_2_runner --prompt "prompt A" --prompt "prompt B"

Otherwise, it will only execute the first prompt.

@cccclai
Copy link
Contributor

cccclai commented Jun 2, 2025

Ah yes, I figure it out. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: qualcomm Changes to the Qualcomm backend delegate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants